﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class manter_cardapio : System.Web.UI.Page
{
    CardapioDTO cardapio_DTO;
    CardapioDAO cardapio_DAO;   
    
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["login"] == "true")
        {
            carregaGrid();
        }
        else
        {
            Response.Redirect("home.aspx");
        }

    }

    private void carregaGrid()
    {
        cardapio_DAO = new CardapioDAO();
        gridCardapios.DataSource = cardapio_DAO.getAll();
        gridCardapios.DataBind();
    }

    protected void cadastrarCardapio(object sender, EventArgs e)
    {
        cardapio_DAO = new CardapioDAO();
        cardapio_DTO = new CardapioDTO();

        cardapio_DTO.Descricao = txt_descricao.Text;

        bool sucesso = cardapio_DAO.insere(cardapio_DTO);
        
        string msg = "";

        if (sucesso)
        {
            msg = "Cadastrado com Sucesso!";
            txt_descricao.Text = String.Empty; 
        }
        else
        {
            msg = "Houve problema no cadastro!";
        }        

        carregaGrid();
    }

    

    protected void atualizarCardapio(object sender, GridViewUpdateEventArgs e)
    {
        cardapio_DAO = new CardapioDAO();
        cardapio_DTO = new CardapioDTO();

        TextBox Idcardapio = (TextBox)gridCardapios.Rows[e.RowIndex].Cells[0].Controls[0];
        TextBox txt_descricao = (TextBox)gridCardapios.Rows[e.RowIndex].Cells[1].Controls[0];

        cardapio_DTO.Idcardapio = Convert.ToInt32(Idcardapio.Text);
        cardapio_DTO.Descricao = txt_descricao.Text;      

        cardapio_DAO.atualiza(cardapio_DTO);

        gridCardapios.EditIndex = -1;
        carregaGrid();

    }

    protected void cancelarCardapio(object sender, GridViewCancelEditEventArgs e)
    {
        gridCardapios.EditIndex = -1;
        carregaGrid();
    }

    protected void editarCardapio(object sender, GridViewEditEventArgs e)
    {
        gridCardapios.EditIndex = e.NewEditIndex;
        carregaGrid();
    }
}